package org.severedelays.android;

import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class AndroidLineResources {
  public static SortedMap<String, Integer> NAMES;
  
  static {
    NAMES = mapIdConstants(R.id.class);
  }
  
  private static SortedMap<String, Integer> mapIdConstants(Class<? extends Object> c) {
    SortedMap<String, Integer> map = new TreeMap<String, Integer>();
    
    Field fileds[] = c.getFields();
    for (Field f : fileds) {
      int modifiers = f.getModifiers();
      if (Modifier.isPublic(modifiers) && 
          Modifier.isStatic(modifiers) && 
          f.getType().equals(Integer.TYPE)) {
        String name = f.getName();
        try {
          int value = f.getInt(null);
          map.put(name, value);
        } catch(Exception e) {
          Log.e("TubeStatus", 
              "Failed to access id constant" + c.getName() + "." + name + '\n' + e);
        }
      }
    }
    return Collections.unmodifiableSortedMap(map);
  }
}
